package cn.itcast.converter;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.ibm.icu.text.DateFormat;
import com.ibm.icu.text.SimpleDateFormat;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;


import java.text.ParseException;
import java.util.Date;

public class StringToDateConverter implements Converter<String, Date> {
     private String partern;

    public void setPartern(String partern) {
        this.partern = partern;
    }

    @Override
    public Date convert(String value) {
        try {
            if (StringUtils.isEmpty(partern)){
                partern="yyyy-MM-dd";
            }
            DateFormat format = new SimpleDateFormat(partern);
            return format.parse(value);
        } catch (ParseException e) {
            throw new IllegalArgumentException("日期格式不对,请输入正确的日期格式,格式为年-月-日");
        }

    }


}
